package com.cet.algorithm.双指针法.移除元素;

/**
 * @program: algorithm
 * @description: 比较含推格的字符串
 * @author: 陈恩涛
 * @create: 2022-03-23 21:16
 **/
public class LC844 {

    public boolean backspaceCompare(String s, String t) {
        char[] charArrayS = s.toCharArray();
        char[] charArrayT = t.toCharArray();

        int slowIndex = 0;
        for (int fastIndex = 0; fastIndex < charArrayS.length; fastIndex++) {
            if (charArrayS[fastIndex] != '#') {
                charArrayS[slowIndex] = charArrayS[fastIndex];
                slowIndex++;
            } else {
                slowIndex = slowIndex > 0 ? slowIndex - 1 : 0;
            }
        }

        int slowIndexT = 0;
        for (int fastIndex = 0; fastIndex < charArrayT.length; fastIndex++) {
            if (charArrayT[fastIndex] != '#') {
                charArrayT[slowIndexT] = charArrayT[fastIndex];
                slowIndexT++;
            } else {
                slowIndexT = slowIndexT > 0 ? slowIndexT - 1 : 0;
            }
        }

        if (slowIndex != slowIndexT) {
            return false;
        }

        for (int i = 0; i < slowIndex; i++) {
            if (charArrayS[i] != charArrayT[i]) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(new LC844().backspaceCompare("ab#bb#c#cd", "abcd"));
    }
}
